package com.lcx.plugin.neteaseCloud.component.handle;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.lcx.plugin.neteaseCloud.music.DataCenter;
import com.lcx.plugin.neteaseCloud.music.api.MusicApi;
import com.lcx.plugin.neteaseCloud.music.entity.LyricEntity;
import com.lcx.plugin.neteaseCloud.music.entity.MusicListEntity;
import com.lcx.plugin.neteaseCloud.music.player.RealMp3Player;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author daxiong
 * @date 2021/10/18 上午9:40
 */
public class SongPlayHandle {

    private static RealMp3Player mp3Player = new RealMp3Player();
    private static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);

    /**
     * 移除当期播放歌曲
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午5:39
     */
    public static void removeCurrentPlayingSong() {
        // 播放下一首歌曲，移除当前播放歌曲
        DataCenter.queueList.remove(0);
        resetPlayAndLyric();
    }

    /**
     * 重置播放和歌曲面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/30 下午4:08
     */
    private static void resetPlayAndLyric() {
        DataCenter.playingSongEntity = null;
        // 刷新面板
        DataCenter.mainPanel.setQueuePanel();
        // 清除歌词面板
        DataCenter.showLyricList.clear();
        DataCenter.lyricList.clear();
        DataCenter.lyricCounter = 0;
        scheduledExecutorService.shutdown();
        DataCenter.mainPanel.setLyric();
    }

    /**
     * 播放下一首
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午5:39
     */
    public static void playNext() {
        if (DataCenter.playMode == 0) {
            // 顺序播放，移除当前播放歌曲
            removeCurrentPlayingSong();
        }
        commonPlayNext();
    }

    /**
     * 直接播放下一首
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 上午11:58
     */
    public static void playNextWithNoMode() {
        // 顺序播放，移除当前播放歌曲
        removeCurrentPlayingSong();
        commonPlayNext();
    }

    /**
     * 从头播放当前歌曲
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 上午11:58
     */
    public static void playAgain() {
        commonPlayNext();
    }

    public static void print() {
        mp3Player.getPlayInt();
    }

    public static void commonPlayNext() {
        if (CollUtil.isEmpty(DataCenter.queueList)) {
            // 队列中没有歌曲了
            resetPlayAndLyric();
            return;
        }
        beginPlaySong(DataCenter.queueList.get(0));
    }

    /**
     * 从头开始播放一首歌曲
     *
     * @param musicListEntity
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午3:48
     */
    public static void beginPlaySong(MusicListEntity musicListEntity) {
        // 播放歌曲
        String songUrl = MusicApi.getMusicUrl(musicListEntity.getSongId());
        System.out.println("songUrl = " + songUrl);
        // 只要是从头开始播放，showLyric都需要清空
        DataCenter.showLyricList.clear();
        DataCenter.lyricCounter = 0;
        scheduledExecutorService.shutdown();
        // 获取歌词
        if (CollUtil.isEmpty(DataCenter.lyricList)) {
            String musicLyric = MusicApi.getMusicLyric(musicListEntity.getSongId());
            String[] split = musicLyric.split("\\n");
            for (String s : split) {
                String[] arr = s.split("]");
                LyricEntity lyricEntity = new LyricEntity(arr[0], arr.length > 1 ? arr[1] : "");
                DataCenter.lyricList.add(lyricEntity);
            }
        }
        if (StrUtil.isNotEmpty(songUrl)) {
            musicListEntity.setSongUrl(songUrl);
            DataCenter.playingSongEntity = musicListEntity;
            // 设置歌曲播放面板
            try {
                DataCenter.mainPanel.setPlayPanel();
            } catch (IOException exception) {
                exception.printStackTrace();
            }

            // 设置播放歌曲的信息
            mp3Player.setUrl(songUrl);
            // 播放歌曲
            mp3Player.play();
            // 播放歌词
            playLyric();
        }
    }

    public static void playLyric() {
        scheduledExecutorService = Executors.newScheduledThreadPool(5);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            if (Objects.isNull(DataCenter.playingSongEntity)) {
                return;
            }
            int playInt = mp3Player.getPlayInt();
            if (CollUtil.isEmpty(DataCenter.showLyricList)) {
                DataCenter.showLyricList.add(DataCenter.lyricList.get(0));
                DataCenter.showLyricList.add(DataCenter.lyricList.get(1));
                DataCenter.showLyricList.add(DataCenter.lyricList.get(2));
                DataCenter.lyricCounter = 2;
            }
            LyricEntity nextLyricEntity = DataCenter.showLyricList.get(2);
            if (nextLyricEntity == null) {
                return;
            }
            if (playInt > nextLyricEntity.getTimeInt()) {
                // 下一句歌词
                DataCenter.lyricCounter++;
                if (DataCenter.lyricCounter < DataCenter.lyricList.size()) {
                    DataCenter.showLyricList.remove(0);
                    LyricEntity nextLyric = DataCenter.lyricList.get(DataCenter.lyricCounter);
                    DataCenter.showLyricList.add(nextLyric);
                }
            }
            DataCenter.mainPanel.setLyric();
        }, 0, 1000, TimeUnit.MILLISECONDS);
    }

    public static void pause() {
        mp3Player.pause();
    }

    public static void stop() {
        mp3Player.stop();
    }


    /**
     * 下一首播放
     *
     * @param musicListEntity
     * @return void
     * @author daxiong
     * @date 2021/10/20 上午11:38
     */
    public static void add2Next(MusicListEntity musicListEntity) {
        // 判断队列中是否有歌曲，如果有，则添加到下一首
        if (CollUtil.isNotEmpty(DataCenter.queueList)) {
            SongQueueHandle.add2Queue(musicListEntity, 1);
        } else {
            // 队列中没有歌曲，则添加到首位
            SongQueueHandle.add2Queue(musicListEntity, 0);
        }
    }

    public static void printProgress() {
        mp3Player.printProgress();
    }
}
